package com.ys.service.finance.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.finance.TbCompanyBankInfoAccount;
import com.ys.entity.finance.TcwFinancialTransactionFlow;
import com.ys.entity.sys.TbCompanyBankInfo;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.finance.FinanceMapper;
import com.ys.mapper.finance.TbCompanyBankInfoAccountMapper;
import com.ys.mapper.hr.HrMapper;
import com.ys.mapper.sys.TbCompanyBankInfoMapper;
import com.ys.service.finance.TbCompanyBankInfoAccountService;
import com.ys.service.flow.YspTaskService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service("tbCompanyBankInfoAccountService")
public class TbCompanyBankInfoAccountServiceImpl implements TbCompanyBankInfoAccountService {
	private static final Logger logger = LoggerFactory.getLogger(TbCompanyBankInfoAccountServiceImpl.class);
	@Autowired
	private HrMapper hrMapper;
	
	@Autowired
	private CheckUtil checkUtil;

	@Autowired
	private FinanceMapper financeMapper;

	@Autowired
	private YspTaskService yspTaskService;

	@Autowired
	private TbCompanyBankInfoAccountMapper tbCompanyBankInfoAccountMapper;

	@Autowired
	private TbCompanyBankInfoMapper tbCompanyBankInfoMapper;
	/*
	 * 新增
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R add(Map<String, Object> params, String subData, Long pid, String companyBankInfoVid, String remark, BigDecimal account) throws Exception {
		//验证
		String msg = yzMsg(params);
		if (msg != null){
			return R.error(msg);
		}

		TbCompanyBankInfoAccount tbCompanyBankInfoAccount = JSON.parseObject(JSON.toJSONString(params), TbCompanyBankInfoAccount.class);
		TbCompanyBankInfo tbCompanyBankInfo = new TbCompanyBankInfo().setVid(tbCompanyBankInfoAccount.getCompanyBankInfoVid()).queryFirst();
		TbCompanyBankInfoAccount entity = new TbCompanyBankInfoAccount();
		entity.setPid(pid);
		entity.setCompanyBankInfoVid(companyBankInfoVid);
		entity.setRemark(remark);
		entity.setAccount(account);
		entity.setStatus(1);
		tbCompanyBankInfoAccountMapper.insert(tbCompanyBankInfoAccount);
		tbCompanyBankInfo.setVid(companyBankInfoVid);
		tbCompanyBankInfoMapper.update(tbCompanyBankInfo);
		return R.ok(AddDto.gotoEdit("/web/sys/tbCompanyBankInfoAccount/toEdit?id=" + tbCompanyBankInfoAccount.getId()));
	}


	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R edit(Map<String, Object> params,String subData) throws Exception {
		//验证
		String msg = yzMsg(params);
		if (msg != null){
			return R.error(msg);
		}
		TbCompanyBankInfoAccount tbCompanyBankInfoAccount = JSON.parseObject(JSON.toJSONString(params), TbCompanyBankInfoAccount.class);

		tbCompanyBankInfoAccountMapper.update(tbCompanyBankInfoAccount);
		return R.ok(AddDto.gotoEdit("/web/sys/tbCompanyBankInfoAccount/toEdit?id=" + tbCompanyBankInfoAccount.getId()));


	}

	@Override
	@Transactional
	public R deleteTrue(Long id) throws Exception {
		
		return R.ok();
	}

	private String yzMsg(Map<String, Object> infoMap){
		//验证导入字段
		String backMsg = "";
		//获取pc字段数据
		List<YsCol> listField = new YsCol()
				.where("A.MenuId = "+10249L)
				.setOrderby("A.SortNo ASC")
				.queryList();
		//验证必填字段的非空；其他字段的长度限制、类型
		try {
			backMsg = checkUtil.checkTableColLengthAndDataType(
					"TB_CompanyBankInfo",
					listField,
					infoMap,
					ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1
			);
			if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
				return backMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
